package org.flexharmony.eclipse.properties;

import java.util.regex.PatternSyntaxException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class PatternsDialog extends Dialog
{
  private Text patternField;
  private boolean updateAction;
  
  private String pattern;

  public PatternsDialog(Shell parent)
  {
    super(parent);
  }

  public PatternsDialog(Shell parent, String pattern)
  {
    super(parent);
    this.pattern = pattern;
  }
  
  public boolean isUpdateAction()
  {
    return updateAction;
  }

  public String getPattern()
  {
    return pattern;
  }

  protected void configureShell(Shell shell)
  {
    super.configureShell(shell);
    shell.setText("Add Class Name Pattern");
  }
  
  protected Control createDialogArea(Composite parent)
  {
    Composite composite = (Composite) super.createDialogArea(parent);
    GridLayout layout = (GridLayout) composite.getLayout();
    layout.numColumns = 2;
    layout.verticalSpacing = 15;

    GridData data = (GridData)composite.getLayoutData();
    data.minimumWidth = 350;
    
    GridData labelData = new GridData();
    labelData.horizontalIndent = 10;
    labelData.verticalIndent = 5;
    
    GridData textData = new GridData();
    textData.widthHint = 300;
    textData.verticalIndent = 5;
    textData.horizontalIndent = 10;
    textData.grabExcessHorizontalSpace = true;
    textData.horizontalAlignment = SWT.FILL;

    Label label = new Label(composite, SWT.RIGHT);
    label.setText("Class Name Pattern:");
    label.setLayoutData(labelData);
    patternField = new Text(composite, SWT.BORDER);
    patternField.setLayoutData(textData);
    if (pattern != null)
    {
      patternField.setText(pattern);
    }
    
    return composite;
  }
  
  protected void createButtonsForButtonBar(Composite parent)
  {
    createButton(parent, -1, "OK", true);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
  }
  
  protected void buttonPressed(int buttonId) 
  {
    switch (buttonId)
    {
      case -1:
        ok();
        break;
      
      default:
        super.buttonPressed(buttonId);
        break; 
    }
  }

  
  private void ok()
  {
    if (patternField.getText().trim().length() == 0)
    {
      MessageDialog.openWarning(getShell(), "Invalid Input", "You must provide a class name pattern.");
    }
    else
    {
      pattern = patternField.getText().trim();
      try
      {
        "test".matches(pattern);
        
        if (pattern.indexOf(PatternMarshaller.DELIMITTER) != -1)
        {
          MessageDialog.openWarning(getShell(), "Invalid Input", "Pattern cannot contain any \"" + PatternMarshaller.DELIMITTER + "\" characters");
        }
        else
        {
          updateAction = true;
          close();
        }
      }
      catch (PatternSyntaxException psexc)
      {
        MessageDialog.openWarning(getShell(), "Invalid Input", "Invalid pattern.");
      }
    }
  }
}
